package com.xlf.common.controller;

import java.util.Date;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.xlf.common.model.Email;
import com.xlf.common.model.Factory;
import com.xlf.common.model.User;
import com.xlf.web.SessionKey;

public class EmailController extends Controller {
	public void login() {
		renderJsp("login.jsp");
	}

	public void loginForm() {
		String username = getPara("username");
		String password = getPara("pwd");
		String from = getPara("from");

		try {
			if (from != null && "factory".equals(from)) {
				Factory factory = Factory.dao.exeFindByName(username, password);
				if (factory != null) {
					int f_id = factory.get("f_id");
					// by zyk 把厂家管理员的fid放入session中
					setSessionAttr("f_id", f_id);
					saveFactory(factory);
					forwardAction("/factory/search");

				} else { // 无此用户
					backtoFrom(from, "登陆账号验证失败，非有效用户");
				}

			} else {
				User user = User.dao.exeFindByName(username, password);
				if (user != null) {
					int status = user.getInt("status");
					int type = user.getInt("type");
					if (status == 0) {
						// 用户已经作废
						backtoFrom(from, "用户已被冻结，禁止登陆");
					} else {
						if (type == 10 || type == 11) {
							// 主材部人员登陆
							saveUser(user);
							forwardAction("/product/index");
						} else if (type == 20 || type == 21) {
							// 设计部人员登陆
							saveUser(user);
							forwardAction("/designer/plan");
						} else {
							// 用户类型不识别
							backtoFrom(from, "用户类型错误(type=" + type + ")");
						}
					}
				} else { // 无此用户
					backtoFrom(from, "登陆账号验证失败，非有效用户");
				}
			}
		} catch (Exception e) {
			backtoFrom(from, "登陆账号验证失败，非有效用户");
		}
	}

	public void logout() {
		removeSessionAttr(SessionKey.USER_TYPE);
		removeSessionAttr(SessionKey.USER);

		String from = getPara("from");
		if (from.equals("designer")) {
			forwardAction("/designer/login");
		} else if (from.equals("material")) {
			forwardAction("/user/login");
		}else if (from.equals("factory")) {
			forwardAction("/factory/login");
		}


	}

	private void saveUser(User user) {
		setSessionAttr(SessionKey.USER, user);
		setSessionAttr(SessionKey.USER_TYPE, user.getInt("type"));
	}

	private void saveFactory(Factory user) {
		setSessionAttr(SessionKey.USER, user);
		setSessionAttr(SessionKey.USER_TYPE, user.getInt("type"));
	}

	private void backtoFrom(String from, String notice) {
		removeSessionAttr(SessionKey.USER);
		removeSessionAttr(SessionKey.USER_TYPE);

		setAttr("notice", notice);

		if (from.equals("designer")) {
			forwardAction("/designer/login");
		} else if (from.equals("material")) {
			forwardAction("/user/login");
		}else if (from.equals("factory")) {
			forwardAction("/factory/login");
		}
	}

	public void index() {
		
		Page<Email> pageRecord = Email.dao.exeSearchSql(getParaToInt("page", 1),
				10);
		setAttr("pageRecord", pageRecord);
		renderJsp("index.jsp");
	}

	public void save() {
		//id 时间 订单号 客户姓名 厂家名称 邮箱地址 发送结果 异常描述
		Email email = getModel(Email.class, "email");
		email.set("createtime", new Date());
		email.set("ordernum", "1");
		email.set("custername", "1");
		email.set("factoryname", "1");
		email.set("email", "1");
		email.set("result", "成功");
		email.set("exceptiondesc", "1");
		email.save();
		index();
	}

	
}
